define(["jquery"],function($){
	var lbox
	,jsonUrl = "json.do"//后台URL
	,baseEjsUrl="ejs/",
	ejs;

	function ejsfn(ejspath){
		return {
			set:function(name,value){
				this[name]=baseEjsUrl+ejspath+"/"+value+".ejs";
			}
		}
	}
	ejs=ejsfn("common");
	ejs.set("alert","alert");
	ejs.set("confirm","confirm");
	ejs.set("pagination","pagination");
	return {
		cmn_ejs:ejs,
		lbox:{
			show:function(){
				if(lbox == null){
					lbox = $('<div  style="left: 0px; top: 0px; width: 100%; height: 100%; opacity: 0.3; zoom: 1; position: fixed; z-index: 9; background: rgb(150, 150, 150);" ></div>');
					$(document.body).append(lbox);
				}
				if($(document.body).index(lbox) == -1){
					$(document.body).append(lbox);
				}
				lbox.show();
			},
			hide:function(){
				lbox.hide();
			}
		},
		ejs:ejsfn,
		post:function(model){
			var r={},callback=function(){},_=this,errback=null;
			r.then=function(call,err){
				callback=call;
				errback=err;
			};
			$.ajax({
				success:function(resp){
					if(resp.ok)callback(resp.data);
					else {
						if(errback)errback(resp.data);
						else _.alert("响应错误",resp.data,"error");
					}
				},
				url:jsonUrl,
				type:"POST",
				data:JSON.stringify(model),
				dataType:"json",
				contentType:"text/json"
			});
			return r;
		},
		alert:function(title,msg,error){
			if(msg==null){
				msg=title;
				title=null;
			}
			var _=this;
			_.lbox.show();
			$(document.body).append(ejs.alert,{title:title,msg:msg,error:error},function(){
				$(".alert .close,.alert .btn").on("click",function(){
					$(this).parents('.alert').remove();
					_.lbox.hide();
				});
			});
		},
		confirm:function(title,msg,callback,type){
			type=type||"warning";
			var _=this;
			_.lbox.show();
			$(document.body).append(ejs.confirm,{title:title,msg:msg,type:type},function(){
				$(".alert .close,.alert [cmd=common-return]").on("click",function(){
					$(this).parents('.alert').remove();
					_.lbox.hide();
				});
				$(".alert [cmd=common-ok]").on("click",function(){
					$(this).parents('.alert').remove();
					_.lbox.hide();
					callback();
				});
			});

		}
	}
});